<?php

class manageCategories {
	var $table='pages';
	var $products=array();
	var $content='';
	var $tblwidth="80%";
	var $page=0;
	var $nrpages=0;
	var $nrows=0;
	var $islist=0;
	
	function manageCategories() {
		if(isset($_GET['action']) ) {
			$data=utils::gpc_check($_GET);
			switch($_GET['action']) {
				case 'list' :
					$this->listCategories($data['order']);
					break;
				case 'edit' :
					$this->editCategory($data['id']);
					break;
				case 'add' :
					$this->addCategory();
					break;
				case 'delete' :
					$this->deleteCategory($data['id']);
					break;
				default :
					$this->listCategories();
			}
		} else {
			$this->listCategories();
		}
		$this->tohtml();
	}
	
	
	function addCategory() {
		global $conn;
		if(isset($_GET['upd'])) {
			$data=utils::gpc_check($_POST);
			$data['pname'] = str_replace(' ', '_', $data['pname']);
			$sql = " 
				INSERT INTO {$this->table} SET
					id_parent='{$data['pcat']}',
					page_name='{$data['pname']}'
				";
			mysql_query($sql,$conn) or die(mysql_error());
			$data['uid'] = mysql_insert_id($conn);
			
			$this->content='<script type="text/javascript">document.location=\''.PAGE_NAME. (isset($data['pcat']) ? '?action=list&order=id_category&explode=' . $data['pcat'] : '') . '\';</script>';
			return;
		} else {
			$cats=utils::getpages();
		    $this->content='<br><br>
				<form name="frm0" action="'.PAGE_NAME.'?action=add&upd" method="POST" onsubmit="return xsubmit(\'frm0\')" enctype="multipart/form-data">
					<tr style="background-color:#bbbbbb;table-layout:fixed">
						<th colspan="2">Add Category</th>
					</tr>
					<tr>
						<td width="30%">Category Name</td>
						<td width="70%"><input type="text" size="40" name="pname" value=""></td>
					</tr>
					<tr>
						<td>Parent Category</td>
						<td>
							<select name="pcat" size="1">
								<option value="0">--select category--</option>	';
							foreach($cats as $k) {
								$this->content.='<option value="'.$k[0].'">'.$k[1].'</option>';
							}
					$this->content.='
						</select>
						</td>
					</tr>
					<tr height="30">
						<td colspan="2" align="center">
							<input type="submit" value="Add" class="zrm_button">
							<input type="button" value="Cancel" class="zrm_button" onclick="document.location=\''.PAGE_NAME.'\' ">
						</td>
					</tr>
				</form>
			<script type="text/javascript">
				addfields("frm0");
				addoblig("frm0",new Array("pname"));
			</script>';
		}
	}

	function deleteCategory ($id) {
		global $conn;
		mysql_query(" DELETE FROM {$this->table} WHERE id_category='{$id}'", $conn);
		$this->content='<script type="text/javascript">document.location=\''.PAGE_NAME.'?action=list&order=id_category&explode='.$_GET['explode'].'\';</script>';
		return;
	}
	
	function editCategory($id) {
		global $conn;
		if(isset($_GET['upd'])) {
			$data=utils::gpc_check($_POST);
			
			mysql_query("
				UPDATE {$this->table} SET
					id_parent='{$data['pcat']}',
					page_name='{$data['pname']}'
				WHERE id_category='{$data['uid']}'", $conn) or die(mysql_error());
			$this->content='<script type="text/javascript">document.location=\''.$data['redir'].'\';</script>';
			return;
		} else {
			$this->tblwidth="70%";
			$sql = " SELECT * FROM {$this->table} WHERE {$this->table}.id_category='{$id}' ";
			$res=mysql_query($sql, $conn);
			if($res!=FALSE && mysql_num_rows($res)!=0) {
				$lin= array_map('htmlspecialchars', array_map('stripslashes', mysql_fetch_assoc($res)));
				mysql_free_result($res);
				$cats=utils::getpages();
				$this->content='<br><br>
				<form name="frm0" action="'.PAGE_NAME.'?action=edit&upd&id='.$id.'" method="POST" enctype="multipart/form-data">
					<input type="hidden" name="uid" value="'.$id.'">
					<input type="hidden" name="redir" value="'.$_SERVER['HTTP_REFERER'].'">
					<tr style="background-color:#bbbbbb;table-layout:fixed">
						<th colspan="2">Edit category</th>
					</tr>
					<tr>
						<td width="30%">Page name</td>
						<td width="70%"><input type="text" size="40" name="pname" value="'.$lin['page_name'].'"></td>
					</tr>';
					
  			$this->content.='
					<tr>
						<td>Parent Category</td>
						<td>
							<select name="pcat" size="1">
								<option value="0">--select category--</option>	';
							foreach($cats as $k) {
								$this->content.='<option value="'.$k[0].'"'.($k[0]==$lin['id_parent'] ? ' selected':'').'>'.$k[1].'</option>';
							}
	  		$this->content.='
						</select>
						</td>
					</tr>';
				$this->content.='
					<tr height="30">
						<td colspan="2" align="center">
							<input type="submit" value="Update" class="zrm_button">
							<input type="button" value="Cancel" class="zrm_button" onclick="document.location=\''.PAGE_NAME.'\' ">
						</td>
					</tr>
				</form>
			<script type="text/javascript">
				addfields("frm0");
				var fields =new Array("pname");
				addoblig("frm0",fields);
			</script>';
			}
		}
	}
		

	function listCategories($order='id_category') {
		global $conn;
		unset($this->cats);
		$this->tblwidth="80%";
		$where=' WHERE NOT id_parent';
		$this->islist=1;
		
		$this->page=(isset($_SESSION['cpage'])?$_SESSION['cpage']:0);
		if(isset($_GET['nav'])) {
			$_SESSION['cpage']=$_GET['nav']-1;
		} else {
			$_SESSION['cpage']=0;
		}
		$this->page=$_SESSION['cpage'];
		
		if(!empty($_GET['criteria'])) {
			$where.=" AND UPPER(page_name) LIKE '%".strtoupper(urldecode($_GET['criteria']))."%'";
		}
		if(!empty($_GET['cat'])) {
			$where.= " AND id_category={$_GET['cat']} ";
		}

		$sql="SELECT COUNT(*) FROM {$this->table} ".$where;
		$res=mysql_query($sql,$conn);
		if($res!=FALSE && mysql_num_rows($res)!=0) {
			$lin=mysql_fetch_row($res);
			mysql_free_result($res);
			$this->nrpages=ceil($lin[0]/ROWS_PER_PAGE);
		} else {
			$this->nrpages=0;
		}
		
		$startfrom = $this->page*ROWS_PER_PAGE;
		$sql="
			SELECT {$this->table}.*
			FROM {$this->table}
			{$where}
			GROUP BY id_category
			ORDER BY {$order}
			LIMIT {$this->page}, ".ROWS_PER_PAGE;
		$res=mysql_query($sql,$conn) or die(mysql_error());
		if($res!=FALSE && mysql_num_rows($res)>0) {
			while($lin=mysql_fetch_assoc($res)) {
				$this->products[]=$lin;
			}
			$this->nrows=mysql_num_rows($res);
			mysql_free_result($res);
		}
		
		$this->content='
				<tr style="background-color:#bbbbbb;">
					<th width="50%">
						<a href="'.PAGE_NAME.'?action=list&amp;order=page_name" class="header">Category Name</a>
					</th>
					<th width="30%">
						<a href="'.PAGE_NAME.'?action=list&amp;order=id_category" class="header">Subcategory Name</a>
					</th>
					<th width="12%">Actions</th>
				</tr>
			';
		if($this->nrows>0) {
			foreach($this->products as $k=>$v) {
				$this->content.='
					<tr >
						<td align="left">
							<a href="'.PAGE_NAME.'?action=list&amp;order='.$order.'&amp;explode='.$v['id_category'].'"><img src="./images/'.(isset($_GET['explode']) && $_GET['explode']==$v['id_category'] ? 'minus.gif': 'plus.gif').'" border="0" alt=""></a>&nbsp;&nbsp;
							'.htmlspecialchars(stripslashes($v['page_name'])).'
						</td>
						<td></td>
						<td align="center">
                        <a href="audio_files.php?action=add&amp;parent=' . $v['id_category'] . '"><img src="./images/duplicate.gif" border="0" alt="Add an MP3 file to this category"></a>&nbsp;
							<a href="'.PAGE_NAME.'?action=edit&amp;id='.$v['id_category'].'"><img src="./images/edit.gif" border="0" alt="Modify"></a>&nbsp;
							<a href="'.PAGE_NAME.'?action=delete&amp;id='.$v['id_category'].'"><img src="./images/delete.gif" border="0" alt="Delete" onclick="if(confirm(\'Are you sure you want to delete this page?\')==true) return true; else return false"></a>&nbsp;
						</td>
					</tr>';
					if(isset($_GET['explode']) && ($_GET['explode']==$v['id_category']) ) {
						$resi=mysql_query("SELECT * FROM {$this->table} WHERE id_parent={$_GET['explode']} ORDER BY page_name ASC",$conn);
						if($resi!==FALSE && mysql_num_rows($resi)) {
							while($row=mysql_fetch_assoc($resi)) {
								$this->content.='
								<tr>
									<td>&nbsp;</td>
									<td>'.htmlspecialchars(stripslashes($row['page_name'])).'</td>
									<td align="center">
                                    <a href="audio_files.php?action=add&amp;parent=' . $row['id_category'] . '"><img src="./images/duplicate.gif" alt="Add an MP3 file to this category"></a>&nbsp;
										<a href="'.PAGE_NAME.'?action=edit&amp;id='.$row['id_category'].'"><img src="./images/edit.gif" border="0" alt="Modify"></a>&nbsp;
										<a href="'.PAGE_NAME.'?action=delete&amp;id='.$row['id_category'].'&amp;explode='.$v['id_category'].'"><img src="./images/delete.gif" border="0" alt="Deleter" onclick="if(confirm(\'Are you sure you want to delete this page?\')==true) return true; else return false"></a>&nbsp;
									</td>
								</tr>';
							}
						}
					}
			}
		}
	}
	
	function tohtml() {
		$content='';
		$cats=utils::getpages();

		if($this->islist) {
		$content.='
		<form name="dispay" method=GET action="'.PAGE_NAME.'">
		<table border="0" cellpadding="2" cellspacing="1" width="'.$this->tblwidth.'" style="border-collapse:collapse;margin-top:40px;table-layout:auto" align="center">
		<tr>
			<td><input type="button" value="Add new" class="zrm_button" onclick="document.location=\''.PAGE_NAME.'?action=add\' "></td>
		</tr>
		</table>
		</form>';
		}
		$content.='<table border="1" cellpadding="3" cellspacing="1" width="'.$this->tblwidth.'" style="border-collapse:collapse;" align="center">';
			
		$content.=$this->content;	
		$content.='</table>';
		echo $content;
	}	
	
	
} 
?>